/*
 * The MIT License
 *
 * Copyright 2015 chronosinfo.com.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * Autor John Vanderson M Lima
 */
package com.chronos.pafecf.modelo;

import com.chronos.pafecf.entidades.EcfPosicaoComponentes;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author usuario
 */
public class EcfPosicaoComponenteDao extends GenericDao {
    private String sql;
    public EcfPosicaoComponentes getEcfPosicaoComponentes(int id) throws SQLException {
        ResultSet rs = execultQuery("select * from ecf_posicao_componentes where id=?", id);
        EcfPosicaoComponentes pos = null;
        if (rs.next()) {
            pos = popularPosicaoComponente(rs);
        }
        rs.close();;
        return pos;
    }

    private EcfPosicaoComponentes popularPosicaoComponente(ResultSet rs) throws SQLException {
        EcfPosicaoComponentes pos = new EcfPosicaoComponentes();
        EcfResolucaoDao dao = new EcfResolucaoDao();
        pos.setAltura(rs.getInt("altura"));
        pos.setEsquerda(rs.getInt("esquerda"));
        pos.setId(rs.getInt("id"));
        pos.setEcfResolucao(dao.getEcfResolucao(rs.getInt("ecf_resolucao")));
        pos.setNome(rs.getString("nome"));
        pos.setTamanhoFonte(rs.getInt("tamanho_fonte"));
        pos.setTexto(rs.getString("texto"));
        pos.setTopo(rs.getInt("topo"));
        return pos;
    }

    public List<EcfPosicaoComponentes> getAllPosicaoComponetes() {
        List<EcfPosicaoComponentes> toReturn = new LinkedList<>();
        ResultSet rs;
        sql =      " select "
                + "P.ID,"
                + "P.NOME, "
                + "P.ALTURA, "
                + "P.LARGURA, "
                + "P.TOPO, "
                + "P.ESQUERDA, "
                + "P.TAMANHO_FONTE, "
                + "P.TEXTO, "
                + "C.ID_ECF_RESOLUCAO "
                + " from "
                + "ECF_POSICAO_COMPONENTES P, ECF_CONFIGURACAO C "
                + " where "
                + "P.ID_ECF_RESOLUCAO=C.ID_ECF_RESOLUCAO";
        try {
            rs = execultQuery(sql);
            while (rs.next()) {
                toReturn.add(popularPosicaoComponente(rs));
            }
            rs.close();
            return toReturn;
        } catch (SQLException ex) {
            return null;
        } finally {

            desconectarDB();
        }

    }

}
